package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Generates a list of CoI and PI users for a Proposal.
 * @author S.N.Fraser
 *
 * $Id: FIND_COI_USERSImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 *
 */
public class FIND_COI_USERSImpl extends TransactionImpl {
    
    /** The path used to locate the Proposal in the OSS database. */
    protected Path proposalPath;
 
    /** Create a FIND_COI_USERSImpl from the supplied request.*/ 
    public FIND_COI_USERSImpl(FIND_COI_USERS request) {
	super(request);
	this.proposalPath = request.getProposalPath();
    }
    
    /**
     * Chenges the password for the Proposal specified in the path.
     * @return Boolean to indicate success.
     */
    public TRANSACTION_DONE exec() { 

	Logger logger = LogManager.getLogger(this);
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    // Fetch the Proposal.
	    Proposal proposal = null;
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "Proposal: "+proposalPath.toString()+
				     " Error reading from database: "+iox);
	    }		

	    // Check it exists.
	    if (proposal == null) {
		logger.log(1, "FIND_COI_USERSImpl", id, "exec",
			   "Proposal: "+proposalPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find Proposal: "+proposalPath.toString());
	    } 
	    
	    // Search all Tags and Users. 
	    Root root = OSS_Utilities.fetchRoot(proposalPath);
	    Tag  tag  = null;  
	    User user = null;
	    
	    Vector list = new Vector();
	    
	    Iterator it1 = root.listAllTags();
	    while (it1.hasNext()) {
		tag = (Tag)it1.next();
		Iterator it2 = tag.listAllUsers();
		while (it2.hasNext()) {
		    user = (User)it2.next();
		    // See if Co-I.
		    if (user.findCoIProposal(proposal.getFullPath()) != null) {		
			list.addElement(new UserDescriptor(user));
		    }
		    // See if P-I.
		    if (user.findProposal(proposal.getName()) != null) {		
			list.addElement(new UserDescriptor(user));
		    }		    
		}
	    }
	    
	    FIND_COI_USERS_DONE done = new FIND_COI_USERS_DONE(id); 
	    done.setSuccessful(true);
	    done.setCoiUsers(list);
	    return done;    	
	    
	} finally {
	    t.commit();
	}
    }
    
}

/** $Log: FIND_COI_USERSImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/19 17:40:58  snf
/** Initial revision
/**
/** Revision 1.1  2000/12/12 18:18:32  snf
/** Initial revision
/**
/** Revision 1.1  2000/09/06 09:30:23  snf
/** Initial revision
/** */
